﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using myMvcBlog.Models;
using SubSonic.Schema;
using System.Data;
using Joel.Net;
using myMvcBlog.Services;
using myMvcBlog.Infrastructure;
using System.Configuration;
using System.Web.Security;

namespace myMvcBlog.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult HomePaging(int page)
        {
            HomeView homeView = new HomeView();
            
            IQueryable<Post> posts = Post.All().Where(p => p.IsPublished).OrderByDescending(post => post.PostedTime)
                                .Skip((page) * ItemsPerPage).Take(ItemsPerPage);

            homeView.Posts = posts.ToPostViews();

            PagerView pager = new PagerView();
            pager.PageCount = (int)Math.Ceiling((double)Post.All().Count() / ItemsPerPage);
            pager.CurrentPage = page;
            pager.ActionName = "HomePaging";
            pager.ControllerName = "Home";

            homeView.PagerView = pager;

            return View("Index",homeView);
        }

        public ActionResult Index()
        {
            HomeView homeView = new HomeView();
            var posts = Post.All().Where(p=>p.IsPublished).OrderByDescending(post => post.PostedTime)
                                .Take(ItemsPerPage);

            homeView.Posts = posts.ToPostViews();

            PagerView pager = new PagerView();
            pager.PageCount = (int)Math.Ceiling((double)Post.All().Count() / ItemsPerPage);
            pager.CurrentPage = 0;
            pager.ActionName = "HomePaging";
            pager.ControllerName = "Home";

            homeView.PagerView = pager;

            return View(homeView);
        }

        

        public ActionResult About()
        {
            return View();
        }

        [ChildActionOnly]
        public ActionResult Sidebar()
        {
            SidebarView sidebar = new SidebarView();

            ITwitterService twitterService = new TwitterService();
            sidebar.Tweets = new List<string>();
            try
            {
                sidebar.Tweets = twitterService.GetTweets().Take(8).ToList();
            }
            catch { }
            sidebar.Categories = myMvcBlog.Category.All().ToList();
            sidebar.RecentPosts = Post.All().Where(p => p.IsPublished).OrderByDescending(post => post.PostedTime).Take(ItemsPerPage).ToList();
            sidebar.RecentComments = myMvcBlog.Comment.All().OrderByDescending(c => c.CommentTime).Take(ItemsPerPage).ToList();

            IDataReader monthlyArchiveReader = new StoredProcedure("dbo.GetMonthlyArchive").ExecuteReader();

            IList<MonthlyArchive> archives = new List<MonthlyArchive>();

            while (monthlyArchiveReader.Read())
            {
                archives.Add(
                        new MonthlyArchive()
                        {
                            Year = monthlyArchiveReader["Year"].ToString(),
                            Month = monthlyArchiveReader["Month"].ToString(),
                            Count = monthlyArchiveReader["Count"].ToString()
                        }
                    );
            }

            sidebar.MonthlyArchives = archives;

            return PartialView("_SidebarPartial", sidebar);
        }

        [HttpGet]
        public ActionResult Logon()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Logon(string name, string password)
        {
            if (name == "jun1st" && password == ConfigurationManager.AppSettings["Password"])
            {
                FormsAuthentication.SetAuthCookie("jun1st", false);
                return RedirectToAction("Index", "Admin");
            }
            else
            {
                return RedirectToAction("Logon");
            }
        }
    }
}
